/*
 * KIMWONSORK@GMAIL.COM
 * COPYRIGHT CRAYONSOFT All rights reserved.
 * 2013오후 9:07:00
 * kws
 */

package com.crayon.account.fragments;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;

import com.crayon.account.dao.DAOFood;
import com.crayon.account.dao.DAOTable;
import com.crayon.account.fragmentspop.FragmentPopBox;
import com.crayon.account.loader.LoaderSQLFoodReOrder;
import com.crayon.account.loader.LoaderSQLFoodSelect;
import com.crayon.account.loader.LoaderSQLTableAppend;
import com.crayon.account.loader.LoaderSQLTables;
import com.crayon.sharemenu.R;
import com.crayon.util.GDB;
import com.crayon.util.UTILSC;
import com.crayon.widget.DraggableGridView;
import com.crayon.widget.OnRearrangeListener;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class FragmentChildMenuAdd extends Fragment
{

	public static FragmentChildMenuAdd onCrateInstance(Date time){
		FragmentChildMenuAdd f = new FragmentChildMenuAdd();
		Bundle args =new Bundle();
		f.setArguments(args);
		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_child_add_menu,container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
        dgv = ((DraggableGridView)getView().findViewById(R.id.vgv));
        button1 = ((Button)getView().findViewById(R.id.button1));
        button2 = ((Button)getView().findViewById(R.id.button2));

        setListeners();
	}

    private void callLoaderAll(){
        LoaderSQLFoodSelect loader = new LoaderSQLFoodSelect(getActivity());
        loader.registerListener(0x00, new Loader.OnLoadCompleteListener<List<DAOFood>>() {
            @Override
            public void onLoadComplete(Loader<List<DAOFood>> loader, List<DAOFood> data) {

                if(getActivity()==null) return;
                if (data != null && data.size() > 0) {
                    poem.clear();
                    for (int i = 0; i < dgv.getChildCount(); i++) {
                        dgv.removeViewAt(i);
                    }

                    for (DAOFood item : data) {
                        String word = item.getSummary();
                        ImageView view = new ImageView(getActivity());
                        if(view!=null){
                            view.setImageBitmap(getThumb(word));
                            dgv.addView(view);
                            poem.add(item);

                        }
                    }
                }
            }
        });
        loader.forceLoad();
    }
    static Random random = new Random();
    DraggableGridView dgv;
    Button button1, button2;
    ArrayList<DAOFood> poem = new ArrayList<DAOFood>();
    private void setListeners() {
        dgv.setOnRearrangeListener(new OnRearrangeListener() {
            public void onRearrange(int oldIndex, int newIndex) {
                DAOFood word = poem.remove(oldIndex);
                if (oldIndex < newIndex)
                    poem.add(newIndex, word);
                else
                    poem.add(newIndex, word);

//                dgv.getChil
//                new LoaderSQLFoodSelect(getActivity(),poem).forceLoad();
                new LoaderSQLFoodReOrder(getActivity(),poem).forceLoad();

            }
        });
        dgv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//                dgv.removeViewAt(arg2);
//                poem.remove(arg2);
                DAOFood item = poem.get(arg2);
                UTILSC.pushActivityComposeFood(getActivity(),item);
                //상세 보기...
            }
        });
        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                UTILSC.pushActivityComposeFood(getActivity(),null);

            }
        });
        button2.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {

//                dgv.g
            }
        });
    }

    private Bitmap getThumb(String s) {
        if(s==null)s=" ";
        int boxWidth= 200;
        int boxHeight=200;
        Bitmap bmp = Bitmap.createBitmap(boxWidth, boxHeight, Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bmp);
        Paint paint = new Paint();
        paint.setColor(Color.rgb(random.nextInt(128), random.nextInt(128), random.nextInt(128)));
        paint.setTextSize(30);
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        canvas.drawRect(new Rect(0, 0, boxWidth,boxHeight), paint);
        paint.setColor(Color.WHITE);
        paint.setTextAlign(Paint.Align.CENTER);
        canvas.drawText(s, 75, 75, paint);
        return bmp;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
//        GDB.log(getClass(), ">>>>onActivityResult " + requestCode);
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onResume() {
        super.onResume();
        callLoaderAll();
    }

}
